/**
此间各个函数 合约 和预期请看core.spec.js
*/
function D(a) {
	if (!(this instanceof D)) return new D(a);
	if (D.type(a, 'Array')) this.datas = a;
	else this.datas = [a];
	this.para = a;
	this.useobj = {
		fns: [],
		status: 0 //number in 0 1 2 in 初始化 中途 结束
	};
}
D.fn = D.prototype = {
	constructor: D
}

function mix(a, b) {
	for (var key in b) a[key] = b[key]
}
D.curry = function (fn) { //函数柯里化
	var args = [].slice.call(arguments, 1);
	return function () {
		var newArgs = args.concat([].slice.call(arguments));
		return fn.apply(this, newArgs);
	}
}
D.type = function (obj, type) {
	return Object.prototype.toString.call(obj) === '[object ' + type + ']'
}
D.mix = D.curry(mix, D)
D.fn.mix = D.curry(mix, D.fn)


function formatdate(pdate,yyyyMMdd) {
	var month = pdate.getMonth() + 1,
		date = pdate.getDate(),
		hours = pdate.getHours(),
		min = pdate.getMinutes(),
		sec = pdate.getSeconds();
	return yyyyMMdd.replace(/yyyy/g, pdate.getFullYear())
		.replace(/yy/g, String(pdate.getFullYear()).substr(2, 2))
		.replace(/MM/g, month >= 10 ? month : "0" + month)
		.replace(/M\*/g, month)
		.replace(/dd/g, date >= 10 ? date : "0" + date)
		.replace(/d\*/g, date)
		.replace(/hh/g, hours >= 10 ? hours : "0" + hours)
		.replace(/h\*/g, hours)
		.replace(/m\*/g, min)
		.replace(/mm/g, min >= 10 ? min : "0" + min)
		.replace(/ss/g, sec >= 10 ? sec : "0" + sec)
		.replace(/s\*/g, sec)
}
D.fn.mix({
	bearray: function () {
		return [].slice.call(this.top())
	},
	bedate:function(yyyyMMdd){
		if(this.type('Number'))return new Date(this.top())
		return new Date(this.top().split('-').join('/'));
		
	},
	top: function () {
		return this.datas[0];
	},
	curry: function () {
		var args = D(arguments).bearray();
		var fn = this.top();
		return function () {
			var newargs = args.concat(D(arguments).bearray());
			return fn.apply(this, newargs);
		}
	},
	type: function (str) {
		if (str == null) return Object.prototype.toString.call(this.para).split(/\[object\s|\]/).join('');
		return D.type(this.para, str);
	},
	each: function (fn) {
		var arr = this.para;
		if (!arr || !arr.length) return;
		if (!this.type('Array')) arr = this.bearray();
		for (var i = 0, l = arr.length, t; i < l; i++) {
			t = arr[i];
			if (fn(t, i) === false) break;
		}
	},
	format: function (a) {
		if (this.type('Date')) {
			a=a||'yyyyMMdd'
			return formatdate(this.top(),a);
		}
		var args = arguments;
		if (args.length == 1 && typeof p1 == "object") args = p1;
		return this.top().replace(/\{([0-9]+)\}/g, function (all, a) {
			return args[a];
		});
	},
	sear: function (fn) {

	},
	use: function (fn) {

	}
})

if (typeof module !== 'undefined') module.exports = D;
